<?

/***************************************************************************
Jupiter Content System @ Jupiterportal.org
 
Copyright (C) 2005-2007 Cosmin Flavius (highstrike@gmail.com)
Copyright (C) 2008-2009 George Mark (gmark@jupiterportal.org)

This program is free software; you can redistribute it and/or modify it under the terms of the GNU 
General Public License as published by the Free Software Foundation; either version 3 of the License, 
or (at your option) any later version.

More Info About The License At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/


function calendar()
{
	$name_function = "b_calendar";

	global $a, $mo, $y, $db, $blocks_db, $template, $language, $config, $misc, $PHP_SELF, $is_loged_in, $is_moderator, $is_administrator, $is_webmaster, $months, $days, $m, $y, $OnlineGuestsCount, $OnlineMembersCount;

	$random_code = md5("$name_function");
	$random_code = substr("$random_code", 0, 5);

	if(!isset($content)) $content = NULL;
	if(!isset($_GET["$random_code"])) $_GET["$random_code"] = NULL;

	if($_GET["$random_code"] == "on") $_SESSION["$random_code"] = "on";
	elseif($_GET["$random_code"] == "off") $_SESSION["$random_code"] = "off";

	$query_string = "?{$_SERVER['QUERY_STRING']}";

	if($query_string == "?" || strpos($query_string, '=on') !== false || strpos($query_string, '=off') !== false) $code_query = "?";
	else $code_query = "$query_string&";

	if(!isset($_SESSION["$random_code"]) || $_SESSION["$random_code"] == "on") $links= "<a href='$code_query$random_code=off'><img src='templates/$template/images/misc/minus.gif' border='0'> {$language['Calendar title']}</a>";
	elseif($_SESSION["$random_code"] == "off") $links = "<a href='$code_query$random_code=on'><img src='templates/$template/images/misc/plus.gif' border='0'> {$language['Calendar title']}</a>";

	$template_block = implode("", file("templates/$template/block.php"));
	$template_block = str_replace("{template}", $template, $template_block);
	$template_block = str_replace("{links}", $links, $template_block);

	if(!isset($_SESSION["$random_code"]) || $_SESSION["$random_code"] == "on")
	{
		//-----------------------------------------------------------------
		//  Some Code parts
		//-----------------------------------------------------------------

		$days = array($language['Calendar day2'],
                      $language['Calendar day3'],$language['Calendar day4'],
                      $language['Calendar day5'],$language['Calendar day6'],
                      $language['Calendar day7'],$language['Calendar day']);
		$months = array($language['Calendar month'], $language['Calendar month'],
                        $language['Calendar month2'], $language['Calendar month3'],
                        $language['Calendar month4'], $language['Calendar month5'],
                        $language['Calendar month6'], $language['Calendar month7'],
                        $language['Calendar month8'], $language['Calendar month9'],
                        $language['Calendar month10'],$language['Calendar month11'],
                        $language['Calendar month12']);

		if ($mo==NULL) $month= date('n');
		else $month = $mo;
		if ($y == NULL)	$year = date('Y');
		else $year = $y;
		$next_year =$year;	
		$prev_year =$year;

		$monthfulltext = date("F", mktime(0, 0, 0, $month, 1, $year));
		$day_in_mth = date("t", mktime(0, 0, 0, $month, 1, $year));

		if($month == 12) {$next_month = 1; $next_year = $year +1;}
		else $next_month = $month + 1; 

		if($month == 1) {$prev_month = 12; $prev_year = $year-1;}
		else $prev_month = $month - 1;

		$result = $db->query("SELECT id, type, day, month, year FROM events WHERE month = '$month' AND year = '$year' ORDER BY 'type'");
		while ($temp = mysql_fetch_array($result))
		{
			$events[$temp['day']][] = $temp['id'];
			$event_info[$temp['id']] = $temp['type'];
			$event_year_db[$temp['id']] = $temp['year'];
		}

		//-----------------------------------------------------------------
		//  Start Display
		//-----------------------------------------------------------------
		$content .= "<tr><td colspan='3'><a href='index.php?n=modules/calendar'><b>".$language['Calendar view calendar']."</a></b>
			    </td><td colspan='4' align='right'><a href='index.php?n=modules/calendar&a=1'><b>".$language['Calendar view list']."</a></b></td>
			</tr>";
		$content .= "<tr class='bottom'><td colspan='7' align='center'><table width='100%' border='0' cellspacing='0' cellpadding='0'><col width='5%'><col width='90%'><col width='5%'><tr class='bottom'><td><a href='index.php?n=modules/calendar&mo=$prev_month&y=$prev_year'>&laquo;Anterior</a></td><td align='center'>{$months[$month]} $year</td><td><a href='index.php?n=modules/calendar&mo=$next_month&y=$next_year'>Siguiente&raquo;</a></td></tr></table></td></tr><tr>";

		foreach($days as $day){ $content .= "<td class='con3' width='14%' align='center'>".$day."</td>";}

		$content .= "</tr><tr>";

		//-----------------------------------------------------------------
		//  For first empty spaces
		//-----------------------------------------------------------------

		$day_of_wk = date("w", mktime(0, 0, 0, $month, 1, $year));

		if ($day_of_wk == 0) $day_of_wk = 7;

		for ($i = 1; $i < $day_of_wk; $i++)
            $content .= "<td class='calendarblank'>&nbsp;</td>";

		//-----------------------------------------------------------------
		//  Show days + events + birthdays
		//-----------------------------------------------------------------

		for ($date_of_mth = 1; $date_of_mth <= $day_in_mth; $date_of_mth++)
		{
			//-----------------------------------------------------------------
			//  Configure stuff first
			//-----------------------------------------------------------------

			$date_no = date("j", mktime(0, 0, 0, $month, $date_of_mth, $year));
			$day_of_wk = date("w", mktime(0, 0, 0, $month, $date_of_mth, $year));

			//-----------------------------------------------------------------
			//  Calculate class
			//-----------------------------------------------------------------
			
			if($month == date('n') && $date_no == date('j'))
                $class = "eventtoday";
			else if ($day_of_wk == 0 || $day_of_wk == 6) $class = "calendarweekend";
            else $class = "calendareventempty";

			//-----------------------------------------------------------------
			//  Show days
			//-----------------------------------------------------------------

			$day_event = "<td class='$class'>$date_no</td>";

			if(isset($events[$date_no]))
			{
				foreach ($events[$date_no] as $key=>$value)
				{
					$titles = $db->getTable("SELECT message FROM events WHERE year = '$year' AND month ='$month' AND day ='$date_no'");
					if($date_no == date('j') && $month == date('n')){ 
						$day_event = "<td class='eventtoday'><a href='$PHP_SELF?n=modules/events&d=$date_no&m=$month&ye=$year'>$date_no<br>";
						if ($titles != NULL) {
							foreach($titles as $title)
								$day_event .= cuttext($title['message'],15)."<br>";
						}
						$day_event .= "</a></td>";
					}
					else
					{
						if($event_info[$value] == 'normal'){
						  $day_event = "<td class='eventselect'><a href='$PHP_SELF?n=modules/events&d=$date_no&m=$month&ye=$year'>$date_no<br>";
						  if ($titles != NULL) {
							foreach($titles as $title)
								$day_event .= cuttext($title['message'],15)."<br>";
						}
						$day_event .= "</a></td>";
						}else {
						      $day_event = "<td class='eventbday'><a href='$PHP_SELF?n=modules/events&d=$date_no&m=$month&ye=$year'>$date_no<br>";
							if ($titles != NULL) {
							      foreach($titles as $title)
								$day_event .= cuttext($title['message'],15)."<br>";
						}
						$day_event .= "</a></td>";
						}
					}
				}
			}

			$content .= $day_event;

			//-----------------------------------------------------------------
			//  For last empty spaces
			//-----------------------------------------------------------------

			if($day_of_wk == 0) $content .= "</tr>";

			if($day_of_wk > 0 && $date_of_mth == $day_in_mth)
			{
				for ($i = $day_of_wk ; $i < 7; $i++) $content .= "<td class='calendarblank'>&nbsp;</td>";

				$content .= "</tr>";
			}
		}
	}

	$template_block = str_replace("{content}", $content, $template_block);
	$output = $template_block;

return $output;
}


function list_view()
{
    global $a, $mo, $y, $db, $blocks_db, $template, $language, $config, $misc,
           $PHP_SELF, $is_loged_in, $is_moderator, $is_administrator,
           $is_webmaster, $months, $days, $m, $y, $OnlineGuestsCount,
           $OnlineMembersCount;
    
    $layout = "";
    
    $months = array("", $language['Name month'],   $language['Name month2'],
                        $language['Name month3'],  $language['Name month4'],
                        $language['Name month5'],  $language['Name month6'],
                        $language['Name month7'],  $language['Name month8'],
                        $language['Name month9'],  $language['Name month10'],
                        $language['Name month11'], $language['Name month12']);
                    
    //-----------------------------------------------------------------
    //  Load the events row
    //-----------------------------------------------------------------
		
    // Added year of event. If no year given, actual year will be set.		
    $year = date('Y');
    $m = date('n');
    $d = date('d');
    
    // Number of next events
    $nevents = 10;
    
    $events_db = $db->getTable(
    "SELECT e.id, e.type, e.user_id, e.user_user, e.start, e.day, e.month, ".
    "e.year, e.end, e.message, e.location, e.title, u.age ".
    "FROM events AS e LEFT JOIN users AS u ON e.user_id = u.id ".
    "WHERE type like 'normal' AND month >= '$m' AND year = '$year' ".
    "ORDER BY month ASC, day ASC, start ASC, end ASC LIMIT $nevents");
    
    foreach($events_db as $idx=>$event)
    {
        //-----------------------------------------------------------------
        // Show only next events
        //-----------------------------------------------------------------
        
        if (($event['month'] == $m && $event['day'] >= $d) ||
        $event['month'] > $m)
        {
            //-----------------------------------------------------------------
            //  Code Layout
            //-----------------------------------------------------------------
            
            $layout .=
            "<tr><td valign='middle' class='con1'>{$event['day']} ".
            "{$language['Calendar of']} {$months[$event['month']]}".
            "<br>";
                
            if ($event['start'] != 0 || $event['end'] != 0)
                $layout .= date("H:i", $event['start'])." - ".
                           date("H:i", $event['end'])."</td>";
            else
                $layout .= "{$language['Events view desc2']}</td>";
            
            // Google Date hack
					
			if ($d <10) $d = "0".$d;
            	
            if ($event['start'] != 0 || $event['end'] != 0)
                $google_date = $year.$m.$d."T".date("Hi", $event['start']).
                               "00Z/".
                               $year.$m.$d."T".date("Hi", $event['end'])."00Z";
            else
                $google_date = $year.$m.$d."T000000Z/".$year.$m.$d."T210000Z";
                
            // End of Google Date hack
				
            $layout .=
            "<td class='con1'>{$event['location']}</td>".
            "<td class='con1'>".parse_message($event['message'])."</td>";
            
            if (isset($is_moderator) || isset($is_administrator) ||
                isset($is_webmaster))
                $layout .=
                "<td valign='bottom' class='con1'><a href='$PHP_SELF?n=modules".
                "/events&a=5&d=$d&m=$m&o={$event['id']}'><img src='templates/".
                "$template/images/buttons/edit.gif' border='0' alt='".
                "{$language['Events view desc8']}'></a>".
                "<a href='$PHP_SELF?n=modules/events&a=3&d=$d&m=$m&o=".
                "{$event['id']}'><img src='templates/$template/images/buttons/".
                "delete.gif' border='0' alt='{$language['Events view desc9']}".
                "'></a></td></tr>";
            else
                $layout .=
                "<td valign='top' class ='con1'><a href='http://www.google.com".
                "/calendar/event?action=TEMPLATE&text=".
                parse_message($event['title'])."&dates=$google_date&details=".
                parse_message($event['message'])."&location=".
                parse_message($event['location'])."&trp=false&sprop=&sprop=".
                "name:Galicia%20Rol' target='_blank'><img src='http://www.".
                "google.com/calendar/images/ext/gc_button6_es.gif' border=0>".
                "</a><br>".
                "<a href='http://www.twitter.com/home?status=".
                str_replace(" ", "+", parse_message($event['title'])).",+el+".
                parse_message($event['day'])."/".parse_message($event['month']).
                "/".parse_message($event['year'])."+en+".
                str_replace(" ", "+", parse_message($event['location'])).
                ".+M&aacute;s+info+en+http://www.galiciarol.org/'>".
                "<img src='images/twitter.jpg' border='0'></a></td></tr>";
		}
	}
    
    //-----------------------------------------------------------------
    //  If no event that day, show error
    //-----------------------------------------------------------------
    
    if ($events_db == FALSE)
        $layout .= "<tr><td class='con1' colspan='3'>".
                   $language['Events view desc']."</td></tr>";
    
    return $layout;	
}

switch($a)
{
    //-----------------------------------------------------------------
    //  Next Events List view
    //-----------------------------------------------------------------
    
    case 1:
    
        echo
        "<table class='main' cellspacing='1' cellpadding='4'>".
        "<tr class='head'>".
        "<td colspan='4'>{$language['Events view title1']}</td></tr>".
        "<tr><td colspan='2'><a href='index.php?n=modules/calendar'>".
        "<b>{$language['Calendar view calendar']}</a></b></td>".
        "<td colspan='2' align='right'><a href='index.php?n=modules/calendar".
        "&a=1'><b>{$language['Calendar view list']}</a></b></td>".
        "</tr>".
        "<tr class='bottom'>".
        "<td width='15%'>{$language['Events view title2']}</td>".
        "<td width='15%'>{$language['Events view title4']}</td>".
        "<td width='82%'>{$language['Events view title3']}</td>".
        "<td width='1%'>{$language['Events view title5']}</td>".
        "</tr>".list_view()."</table>";
        
    break;
    
    //-----------------------------------------------------------------
    //  Show Calendar
    //-----------------------------------------------------------------
	
    default:
    
        echo calendar();
}
?>
